<?php

/* 积分管理控制器 */
class IntegralApp extends BackendApp
{
	var $_member_mod;
	var $_integral_mod;
    function __construct()
    {
        $this->IntegralApp();
    }

    function IntegralApp()
    {
        parent::__construct();
		$this->_member_mod =& m('member');
		$this->_integral_mod = &m('integral');
    }
    function index()
    {
		$conditions = $this->_get_query_conditions(array(
            array(
                'field' => $_GET['field_name'],
                'name'  => 'field_value',
                'equal' => 'like',
            ),
        ));
		$page = $this->_get_page();
        $users = $this->_member_mod->find(array(
            'fields' => 'member.user_id,member.user_name,ig.*',
			'join'   => 'has_integral',
            'conditions' => '1=1' . $conditions,
            'limit' => $page['limit'],
            'order' => "amount desc",
            'count' => true,
        ));
		$this->assign('users',$users);
		$page['item_count'] = $this->_member_mod->getCount();
        $this->_format_page($page);
        $this->assign('filtered', $conditions? 1 : 0); //是否有查询条件
        $this->assign('page_info', $page);
		$this->assign('query_fields', array('user_name' => LANG::get('user_name')));
        $this->display('integral.index.html');
    }
	function setting()
	{
		$model_setting = &af('settings');
        $setting = $model_setting->getAll(); //载入系统设置数据
        if (!IS_POST)
        {
			$sgrade_mod =& m('sgrade');
			$integral_mod =& m('integral');
			$sgrades = $sgrade_mod->find(array('fields'=>'grade_id,grade_name'));
			foreach($sgrades as $key=>$val){
				$sgrades[$key]['buying_integral'] = $integral_mod->_get_sys_setting(array('buying_integral',$val['grade_id']));
			}

        	$this->assign('sgrades', $sgrades);
            $this->assign('setting', $setting['integral_manage']);
            $this->display('integral.setting.html');
        }
        else
        {
            $data['integral_enabled']  	= intval($_POST['integral_enabled']);
			$data['exchange_rate']     	= empty($_POST['exchange_rate'])? 0.1 : round($_POST['exchange_rate'],2);
			$data['register_integral'] 	= floatval($_POST['register_integral']);
			$data['sign_in_integral'] 	= floatval($_POST['sign_in_integral']);
			$data['open_integral'] 		= floatval($_POST['open_integral']);
			
			foreach($_POST['buying_integral'] as $k=>$v)
			{
				$data['buying_integral'][$k] = $v;
				if($v > 1 || $v < 0) {
					$data['buying_integral'][$k] = 0;
				}
			}
			
            $model_setting->setAll(array('integral_manage' => $data));
            $this->show_message('edit_integral_setting_successed');
        }
	}
	
	function recharge()
	{
		$user_id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		
		if(!$user_id || !$this->_member_mod->get(array('conditions'=>'user_id='.$user_id, 'fields'=> 'user_id'))) {
			$this->show_warning('user_empty');
			return;
		}
		
		if(!IS_POST) 
		{
			$this->display('integral.form.html');
		}
		else
		{
			$flow	 = trim($_POST['flow']);
			if(!in_array($flow, array('add', 'minus'))) {
				$flow = 'minus';
			}
			
			$data = array(
				'user_id' => $user_id,
				'type' 	  => 'admin_handle',
				'flow'    => $flow,
				'amount'  => floatval($_POST['amount']),
				'flag'    => html_script($_POST['flag'])
			);
			$this->_integral_mod->update_integral($data);
			$this->show_message('edit_ok', 'back_list', 'index.php?app=integral');
		}
	}
	function record()
	{
		$user_id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		
		if(!$user_id){
			$this->show_warning('user_empty');
			return;	
		}
		$page = $this->_get_page(10);
		
		$integral_log_mod = &m('integral_log');
		$integral_log = $integral_log_mod->find(array(
			'conditions'=>'user_id='.$user_id,
			'order'     =>'add_time DESC',
			'limit'     =>$page['limit'],
			'count'     =>true
		));
		foreach($integral_log as $key=>$val)
		{
			$integral_log[$key]['state']= $this->_integral_mod->status($val['state']);
			$integral_log[$key]['type'] = Lang::get($val['type']);
		}
        $page['item_count'] = $integral_log_mod->getCount();   //获取统计的数据
        $this->_format_page($page);
        $this->assign('page_info', $page);          //将分页信息传递给视图，用于形成分页条
        $this->assign('integral_log', $integral_log);	
		$this->display('integral.record.html');
	}
}

?>
